﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoPortal.Areas.Admin.Models;
using TaoPortal.Entity;

namespace TaoPortal.Areas.Admin.Controllers
{
    public class FileManageController : BaseController
    {
        public ActionResult Index()
        {
            FileManageModel model = new FileManageModel();
            model.DomainUrl = "http://www.taoportal.com";
            return View(model);
        }

        public ActionResult AjaxGetClass(string id)
        {
            FileManageModel model = new FileManageModel();
            List<TreeData> list = model.GetClass(id);
            return Content(SerializeJson(list));
        }

        public ActionResult AddClass()
        {
            FileClassModel model = new FileClassModel();
            model.InitFileClass();
            return View(model);
        }
        [HttpPost]
        public ActionResult AddClass(FileClassModel model)
        {
            if (ModelState.IsValid)
            {
                model.Add();
                return Content("<script>parent.CloseWin()</script>");
            }
            else
            {
                return View(model);
            }
        }
        public ActionResult EditClass(string id)
        {
            FileClassModel model = new FileClassModel();
            model.InitFileClass();
            model.InitClass(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult EditClass(FileClassModel model)
        {
            if (ModelState.IsValid)
            {
                model.Edit();
                return Content("<script>parent.CloseWin()</script>");
            }
            else
            {
                return View(model);
            }
        }
        [HttpPost]
        public ActionResult DeleteClass(string id)
        {
            FileClassModel model = new FileClassModel();
            model.Delete(id);
            return Json(new { result = true });
        }
        public ActionResult Upload()
        {
            UploadModel model = new UploadModel();
            model.InitData();
            return View(model);
        }

        [HttpPost]
        public ActionResult Upload(UploadModel model)
        {
            if (!ModelState.IsValid)
            {
                model.InitData();
                return View(model);
            }
            else
            {
                if (Request.Files.Count > 0 && Request.Files[0] != null && Request.Files[0].ContentLength > 0)
                {
                    var file = Request.Files[0];
                    model.FData = new Byte[file.ContentLength];
                    file.InputStream.Position = 0;
                    file.InputStream.Read(model.FData, 0, file.ContentLength);
                    model.FLength = file.ContentLength;
                    model.FName = System.IO.Path.GetFileName(file.FileName);
                    model.FExtName = System.IO.Path.GetExtension(file.FileName);
                    model.FType = file.ContentType;
                    model.SaveFile();
                }
                return Content("<script>parent.SubmitSuccess()</script>"); 
            }
        }

        public ActionResult Download(string id)
        {
            FileManageModel model = new FileManageModel();
            TP_SysFile file = model.Download(id);
            return View();
        }

        [HttpPost]
        public ActionResult AjaxGetList(FileManageModel model)
        {
            var data = model.GetFilePageList();
            return Content(SerializeJson(new { results = data.TotalItemCount, rows = data }));
        }

        public ActionResult SelectFile()
        {
            FileManageModel model = new FileManageModel();
            model.InitClass();
            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            FileManageModel model = new FileManageModel();
            model.Delete(id);
            return Json(new { result = true });
        }

    }
}
